﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UMLDesigner
{
    public partial class MethodsWindow : Form
    {
        List<ParameterData> parametros;
        Boolean modificar = false;
       
        public MethodsWindow()
        {
            InitializeComponent();
            listBoxReturnType.DataSource = DataType.types;
            listBoxVisibility.DataSource = VisibilityType.GetTypes();
            parametros = new List<ParameterData>();
        }

        public MethodsWindow(MethodData met):this()
        {
            modificar = true;
            textBoxName.Text = met.GetName();
            for (int i = 0; i < met.GetParameters().Count; i++)
            {
                parametros.Add(met.GetParameters()[i]);
                listBoxParameters.Items.Add(met.GetParameters()[i].GetUMLRepresentation());
            }
            listBoxReturnType.Text = met.GetReturnType();
            listBoxVisibility.Text = met.GetVisibility();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAnaydirParametro_Click(object sender, EventArgs e)
        {
            ParametrosVentana anyadirParametro = new ParametrosVentana();
            anyadirParametro.Owner = this;
            anyadirParametro.Show(this);
        }

        public void AnyadirParametro(string nombre, string tipo)
        {
            ParameterData parametro = new ParameterData(nombre, tipo);
            parametros.Add(parametro);
            listBoxParameters.Items.Add(parametro.GetUMLRepresentation());
        }

        public void ModificarParametro(string nombre,string tipo)
        {
            int indice = listBoxParameters.SelectedIndex;
            parametros[indice].SetName(nombre);
            parametros[indice].SetType(tipo);
            listBoxParameters.Items[indice]=parametros[indice].GetUMLRepresentation();
        }

        private void btnEditarParametro_Click(object sender, EventArgs e)
        {
            if (listBoxParameters.SelectedItem != null)
            {

                int indice = listBoxParameters.SelectedIndex;
                ParametrosVentana editarParametro = new ParametrosVentana(parametros[indice]);
                editarParametro.Owner = this;
                editarParametro.Show(this);
            }
        }


        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (!modificar)
            {
                WindowsManager.AddMethodToForm(listBoxVisibility.Text, textBoxName.Text, parametros, listBoxReturnType.Text);
            }
            else
            {
                WindowsManager.ModifyMethod(listBoxVisibility.Text, textBoxName.Text, parametros, listBoxReturnType.Text);
            }
            
            Close();
        }

        private void btnEliminarParametro_Click(object sender, EventArgs e)
        {
            if (listBoxParameters.SelectedItem != null)
            {
                for (int i = 0; i < parametros.Count; i++)
                {

                    if (parametros[i].GetUMLRepresentation() == listBoxParameters.Text)
                    {
                        parametros.Remove(parametros[i]);
                    }
                }

                listBoxParameters.Items.Remove(listBoxParameters.SelectedItem);

            }
        }

       
    }
}
